﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DBService
{
	public partial class DlgChangePassword : System.Windows.Forms.Form
	{
		public DlgChangePassword(string userName)
		{
			this.InitializeComponent();
			this.txtUser.Text = userName;
		}

		private void label2_Click(object sender, EventArgs e)
		{
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.txtUser.Text.Trim() != "" && this.txtOldPw.Text.Trim() != "" && this.txtPw.Text.Trim() != "" && this.txtPw1.Text.Trim() != "")
			{
				try
				{
					if (this.txtPw.Text.Trim() != this.txtPw1.Text.Trim())
					{
						this.txtPw1.Text = "";
						this.txtPw1.Focus();
						MessageBox.Show("重复密码和新密码不匹配。");
						return;
					}
					DbUser user = DbUser.GetUser(this.txtUser.Text.Trim(), BimServerDB.Default);
					if (user.Password.Equals(DbUser.ToMd5(this.txtOldPw.Text.Trim())))
					{
						user.ChangePassword(this.txtPw1.Text.Trim());
						MessageBox.Show("密码修改成功。");
						base.Close();
					}
					else
					{
						MessageBox.Show("原密码不正确。");
					}
					return;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
					return;
				}
			}
			MessageBox.Show("请正确填写密码。");
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void DlgChangePassword_Load(object sender, EventArgs e)
		{
		}

		private void txtUser_TextChanged(object sender, EventArgs e)
		{
		}

		private void cbxUser_SelectedIndexChanged(object sender, EventArgs e)
		{
		}
	}
}
